<template>
    <div class="app-container">
        <!--工具栏-->
        <div class="head-container">
            <div>
                <!-- 搜索 -->
                <el-input v-model="query.blurry" clearable size="small" placeholder="订单号或者订单金额模糊搜索" style="width: 220px;"
                             class="filter-item" @keyup.enter.native="toQuery" />
                <el-select v-model="query.uid" clearable class="filter-item" style="width: 150px" placeholder="请选择用户名称" @change="crud.toQuery" filterable>
                    <el-option
                    v-for="(item, index) in users"
                    :key="item.username + index"
                    :label="item.username"
                    :value="item.uid"
                    />
                </el-select>
                <el-select v-model="query.agentId" clearable class="filter-item" style="width: 150px" 
                                        placeholder="请选择代理商" @change="crud.toQuery" filterable>
                    <el-option
                    v-for="(item, index) in agents"
                    :key="item.phone + index"
                    :label="item.phone"
                    :value="item.id"
                    />
                </el-select>
                <rrOperation :crud="crud"/>
            </div>
            
            <!--表格渲染-->
            <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                      @selection-change="crud.selectionChangeHandler">
                <el-table-column prop="orderNo" label="订单号" min-width="160px"/>
                <el-table-column prop="userName" label="用户名称" min-width="100px"/>
                <el-table-column prop="agentName" label="代理商" min-width="100px"/>
                <el-table-column prop="amount" label="订单金额"/>
                <el-table-column prop="userAmount" label="用户返还金额" min-width="100px"/>
                <el-table-column prop="agentAmount" label="代理商佣金"/>
                <el-table-column prop="companyAmount" label="公司收入金额" min-width="100px"/>
                <el-table-column prop="isTransfer" label="已转账" min-width="60px">
                    <template slot-scope="scope">
                        <div>
                            <el-tag v-if="scope.row.isTransfer === 0" :type="'danger'">否</el-tag>
                            <el-tag v-else :type="'success'">是</el-tag>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="160px" align="center" fixed="right">
                  <template slot-scope="scope">
                    <el-button size="mini" style="margin-right: 2px" type="text">
                      <router-link :to="'transfer/' + scope.row.id">
                        审核待转账信息
                      </router-link>
                    </el-button>
                  </template>
                </el-table-column>

            </el-table>
            <!--分页组件-->
            <pagination/>
        </div>
    </div>
</template>

<script>
    import { getAllAgent } from '@/api/agentMember'
    import { getAllMemberUser } from '@/api/memberUser'
    import { mapGetters } from 'vuex'
    import { getToken } from '@/utils/auth'
    import crudRechargeOrder from '@/api/rechargeOrder.js'
    import CRUD, {crud, form, header, presenter} from '@crud/crud'
    import rrOperation from '@crud/RR.operation'
    import crudOperation from '@crud/CRUD.operation'
    import udOperation from '@crud/UD.operation'
    import pagination from '@crud/Pagination'
    import MaterialList from "@/components/material";

    // crud交由presenter持有
    const defaultCrud = CRUD({
        title: '充值订单',
        url: 'api/rechargeOrder',
        sort: 'id,desc',
        params: {isTransfer: 0},
        crudMethod: {...crudRechargeOrder}
    })
    const defaultForm = {id: null, orderNo: null, uid: null, userName: null, agentId: null, agentName: null, amount: null, userAmount: null, agentAmount: null, companyAmount: null, isTransfer: null, isDel: null, createTime: null, updateTime: null}
    export default {
        name: 'AgentMember',
        components: {pagination, crudOperation, rrOperation, udOperation , MaterialList},
        mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
        computed: {
            ...mapGetters([
            'ossUploadApi'
            ])
        },
        data() {
            return {
                title: '充值订单信息',
                users: [],
                agents: [],
                isAdd: false,
                dialog: false,
                icon: 'el-icon-refresh',
                url: '', 
                headers: { 'Authorization': getToken() },
                dialogImageUrl: '', dialogVisible: false, fileList: [], files: [], newWin: null,
                permission: {
                    add: ['admin', 'rechargeOrder:list'],
                    edit: ['admin', 'rechargeOrder:list'],
                    del: ['admin', 'rechargeOrder:list']
                },
                rules: {
                    agentName: [
                        { required: true, message: '代理商名称不能为空', trigger: 'blur' },
                        { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
                    ], 
                    phone: [
                        { required: true, message: '手机号码不能为空', trigger: 'blur' },
                        { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                    ]
                },
                queryTypeOptions: [
                    {
                        key: 'agentName',
                        display_name: '代理商名称'
                    }, 
                    {
                        key: 'phone',
                        display_name: '手机号码'
                    }
                ]
            }
        },
        created() {
          //初始化按钮显示或者隐藏
          this.crud.optShow = { add: true, edit: false, del: false, download: false }
        },
        watch: {
        },
        mounted:function(){ //在页面加载参数结束后执行某个函数
            this.$nextTick(function(){
                //调用需要执行的方法
                this.getMemberUser()
                this.getAgent()
            })      
        },
        methods: {
            // 获取数据前设置好接口地址
            [CRUD.HOOK.beforeRefresh]() {
                const query = this.query
                if (query.type && query.value) {
                    this.crud.params[query.type] = query.value
                } else {
                    delete this.crud.params.agentName
                    delete this.crud.params.phone
                }
                return true
            }, // 新增与编辑前做的操作
            [CRUD.HOOK.afterToCU](crud, form) {
            },
            getAgent() {
                getAllAgent().then(res => {
                    this.agents = res
                }).catch(() => { })
            },
            getMemberUser() {
                getAllMemberUser().then(res => {
                    this.users = res
                }).catch(() => { })
            },
            beforeUpload(file) {
                const isLimit = file.size / 1024 / 1024 < 10
                if (!isLimit) {
                    this.$message.error('上传文件的大小必须小于10MB!')
                }
                return isLimit
            },
            handleSuccess(response, file, fileList) {
                const uid = file.uid
                const id = response.id
                this.form.imgUrl = response.url
                this.files.push({ uid, id })
            },
            handleBeforeRemove(file, fileList) {
                this.form.imgUrl = ''
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url
                this.dialogVisible = true
            },
            // 监听上传失败
            handleError(e, file, fileList) {
                const msg = JSON.parse(e.message)
                this.crud.notify(msg.message, CRUD.NOTIFICATION_TYPE.ERROR)
            },
            // 刷新列表数据
            doSure() {
                this.fileList = []
                this.dialogVisible = false
                this.dialogImageUrl = ''
                this.dialog = false
                //this.crud.toQuery()
            }
        }
    }


</script>

<style scoped>
    .table-img {
        display: inline-block;
        text-align: center;
        background: #ccc;
        color: #fff;
        white-space: nowrap;
        position: relative;
        overflow: hidden;
        vertical-align: middle;
        width: 32px;
        height: 32px;
        line-height: 32px;
    }
    
    #divUpload {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
    }
</style>
